package train.DAY623;

import java.util.ArrayList;
import java.util.List;

public class LeetCode623 {
    public static void main(String[] args) {
//  int[] a={1,4,2,5,3};
//        System.out.println(sunLength(a));

//        int[] nums={0};
//        moveZero(nums);

//        int[][] img={{100,200,100},{200,50,200},{100,200,100}};
//        int[][] ints = imageSmooth(img);
//        for (int[] anInt : ints) {
//            for (int i = 0; i < anInt.length; i++) {
//                System.out.print(anInt[i]+"\t");
//            }
//            System.out.println();
//        }
//        String s = converTo(2147483647);
//        System.out.println(s);
        String babad = longPal1("cbbd");
        System.out.println(babad);
    }
    //奇数长度子数组的和
    public static int sunLength(int[] arr){
        int i=1;
        int sum=0;
        while(i<=arr.length){
            for (int j = 0; j < arr.length; j++) {
              if(arr.length-j>=i){
                  for (int k = j; k <i+j ; k++) {
                      sum+=arr[k];
                  }

              }else break;
            }
            i+=2;
        }
        return sum;
    }

    //移动零
    public static void moveZero(int[] nums){
        int temp=0,j=0;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == 0) ;
            else {
                while (temp <= i && temp < nums.length) {
                    if (nums[temp] != 0)
                        temp++;
                    else break;
                }
                if (temp <= i && temp < nums.length) {
                    nums[temp] = nums[i];
                    nums[i] = 0;
                    temp++;
                }
            }
        }
    }

    //最富有的资产总量
    public static int maxWealth(int[][] accounts){
        int max=0;
        int sum=0;
        for (int i = 0; i < accounts[0].length; i++) {
            max+=accounts[0][i];
        }
        if(accounts.length==1)
            return max;
        else{
            for (int i = 1; i < accounts.length; i++) {
                for (int j = 0; j < accounts[i].length; j++) {
                    sum+=accounts[i][j];
                }
                if(sum>max) max=sum;
            }
        }
        return max;
    }

    //Excel表列
    public static String converTo(int columnNumber){
        int yu=0;
        int shen=0;
        StringBuffer buffer = new StringBuffer();
        while(columnNumber!=0){
          yu=(columnNumber-1)%26;
          buffer.append((char)('A'+yu));

           columnNumber=(columnNumber-1)/26;
      }

        return String.valueOf(buffer.reverse());
    }
   //Excel表列序号-----未完成
    public int titleTo(String columnTitle){
        int sum=0;
        for (int i = 0; i <columnTitle.length() ; i++) {
            sum+=columnTitle.charAt(i)-'A';
        }
        return 0;

    }

    //图片平滑器
    public static int[][] imageSmooth(int[][] img){
        int num=0;
        int sum=0;

        int[][] a=new int[img.length][img[0].length];
        for (int i = 0; i < img.length; i++) {
            for (int j = 0; j < img[0].length; j++) {
               if(sumDou(i,j,img)){
                   sum+=img[i][j];
                   num++;
               }
               if(sumDou(i-1,j-1,img)){
                   sum+=img[i-1][j-1];
                   num++;
               }
               if(sumDou(i-1,j,img)){
                   sum+=img[i-1][j];
                   num++;
               }
               if(sumDou(i-1,j+1,img)){
                   sum+=img[i-1][j+1];
                   num++;
               }
               if(sumDou(i,j-1,img)){
                   sum+=img[i][j-1];
                   num++;
               }
               if(sumDou(i,j+1,img)){
                   sum+=img[i][j+1];
                   num++;
               }
               if(sumDou(i+1,j-1,img)){
                   sum+=img[i+1][j-1];
                   num++;
               }
               if(sumDou(i+1,j,img)){
                   sum+=img[i+1][j];
                   num++;
               }
               if(sumDou(i+1,j+1,img)){
                   sum+=img[i+1][j+1];
                   num++;
               }
               sum=sum/num;
               a[i][j]=sum;
               sum=0;
               num=0;
            }
        }
        return a;
    }

    public static boolean sumDou(int x,int y,int[][] img){

        if(x>=0&&x<= img.length-1&&y>=0&&y<=img[0].length-1){
            return true;
        }
        else return false;
    }

    //最长回文子串
    public static String longPal(String s){
     int temp=0;
     int a=0,b=0;
     int x=0,y=0;
     int sum=0;
     boolean flag=false;
        for (int i = 1; i < s.length(); i++) {
            for (int j = i-1; j >=0; j--) {
                if(s.charAt(i)==s.charAt(j)){
                    a=i;
                    b=j;
                    while(b<=a){
                        if(s.charAt(a)==s.charAt(b))
                        {
                            a--;
                            b++;
                            flag=true;
                        }else
                            break;

                    }
                }
                if(flag) {
                    if (b > a) {
                        if (sum < i - j) {
                            x = i;
                            y = j;
                            sum = i - j;
                        }
                    }
                }
                flag=false;
            }

        }
        String s1 = s.substring(y, x + 1);
        return s1;
    }
    
    //最长回文子串--动态规划完成
    public static String longPal1(String s){
        char[] array = s.toCharArray();
        int x=0,y=0;
        int sum=0;
        for (int i = 1; i < array.length ; i++) {
            for (int j = 0; j < array.length-i; j++) {
                if(huiwen(j,j+i,array)){
                    if(sum<i) {
                        x = j;
                        y = j + i;
                        sum = i;
                    }
                }
            }
        }

        StringBuffer buffer = new StringBuffer();
        for (int i = x; i <=y ; i++) {
            buffer.append(array[i]);
        }

        return buffer.toString();
    }

    public static boolean huiwen(int i,int j,char[] nums){
        while(i<=j){
            if(nums[i]!=nums[j])
                return false;
            else {
            i++;
            j--;
            }
        }
        return true;
    }
    
  //电话号码字母组合

    public static   List<String> letterCom(String digits){
        String[] a={"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        String b=null;
        List<String> list = new ArrayList<>();
        int c=0;
        int max=0;
        for (int i = 0; i < digits.length(); i++) {
            c=digits.charAt(i)-48;
            b=a[c-2];
            list.add(b);
        }
        max=list.get(1).length();
        for (int i = 2; i < list.size() ; i++) {
            if(list.get(i).length()>max) max=list.get(i).length();
        }
     String[][] d=new String[list.get(0).length()][max];
        return list;

    }



}
class Node{
    public int val;
    public List<Node> children;
    public Node(){}
    public Node(int _val){
        val=_val;
    }
    public Node(int _val,List<Node> _children){
        val=_val;
        children=_children;
    }

}
